package com.tlx.app.geekbang;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.io.FileUtil;

public class GeekBangVideo {

    private static String CMD_BASE = "D:\\work\\ffmpeg\\bin\\ffmpeg -i \"%s\" -vcodec copy -acodec copy -absf aac_adtstoasc \"%s\"";

    public static void main(String[] args) throws Exception {
        String jsonFile = "D:/0/a.txt";
        String videDir = "D:/0/面试/";
        String batFile =  "D:/0/面试.bat";

        // 校验和初始化
        new File(videDir).mkdirs();
        if (new File(batFile).exists()) {
            throw new RuntimeException("bat文件存在：" + batFile);
        }

        // 解析json，成为命令行
        List<String> cmdList = new ArrayList<String>();

        String text = FileUtils.readFileToString(new File(jsonFile));
        JSONObject jo = JSONObject.parseObject(text);

        JSONObject data = (JSONObject) jo.get("data");
        JSONArray list = (JSONArray) data.get("list");

        for (Object object : list) {
            JSONObject ke = (JSONObject) object;
            JSONObject video_media_map = (JSONObject) ke.get("video_media_map");
            JSONObject hd = (JSONObject) video_media_map.get("hd");
            String url = (String) hd.get("url");

            String article_title = (String) ke.get("article_title");

            // 不能有|字符
            article_title = StringUtils.replace(article_title, " ", "");
            article_title = StringUtils.replace(article_title, "|", "_");

            String cmd = String.format(CMD_BASE, url, videDir + article_title + ".mp4");

            System.out.println(cmd);
            FileUtil.appendLines(Arrays.asList(cmd), batFile, "gbk");
        }

    }
}
